package com.heima.wemedia.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.media.pojos.WmUser;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.springframework.stereotype.Service;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    /**
     * 保存用户信息到媒体表中
     * @param wmUser
     * @return
     */
    @Override
    public ResponseResult insert(WmUser wmUser) {
        //1. 效验参数
        if (ObjectUtil.isEmpty(wmUser) || wmUser.getName() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2. 将用户数据保存到媒体表中
        save(wmUser);

        //3. 返回保存成功
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据用户id在媒体表中查询数据
     * @param id
     * @return
     */
    @Override
    public ResponseResult findById(Integer id) {
        //1. 效验参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2. 根据名称查询用户信息
        LambdaQueryWrapper<WmUser> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(WmUser::getId,id);

        WmUser user = getOne(lambdaQuery);
        //3. 将查询到的用户信息返回
        return ResponseResult.okResult(user);
    }
}
